﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

public partial class seal_seal_report_represen_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }

        if (!loginModel.isJimukyoku() && !loginModel.isOperator())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        try
        {
            if (!IsPostBack)
            {
                string area = Resources.Resource.area;
                string[] areaArray = area.Split(',');
                for (int i = 0; i < areaArray.Length; i++)
                {
                    ListItem item = new ListItem();
                    item.Value = (i + 1) + "";
                    item.Text = areaArray[i];
                    HALL_PREF_CODE.Items.Insert(i + 1, item);
                }

                TrnReporth report_h = (TrnReporth)Session["CHECKED_REPORT_H"];
                MST_UnionPersister dao1 = new MST_UnionPersister();
                MST_Union_BranchPersister dao2=new MST_Union_BranchPersister();
                MST_TraderPersister dao3 = new MST_TraderPersister();
                MstUnion union = dao1.getDetail(report_h.UnionCode);
                report_h.TraderRegNo1 = StringUtil.isEmpty(union.TraderRegNo)?"": union.TraderRegNo.Substring(0,2);
                report_h.TraderRegNo2 =  StringUtil.isEmpty(union.TraderRegNo)?"":union.TraderRegNo.Substring(2,5);
                report_h.TraderRegNo = union.TraderRegNo;
                if (report_h == null)
                {
                    // 関係ないページからの遷移
                    Response.Redirect("seal_report_represen.aspx");
                    return;
                }
                else
                {
                    // seal_report_represen.jsp からの遷移
                    report_h.EstCompClss = TrnReporth.EST_COMP_CLSS_SELF;
                }

                SEAL_REPORT_REPRESEN_RCV_DATE_Y.Text =((DateTime) report_h.FaxRcvDate).ToString("yy");
                SEAL_REPORT_REPRESEN_RCV_DATE_M.Text = ((DateTime)report_h.FaxRcvDate).ToString("MM");
                SEAL_REPORT_REPRESEN_RCV_DATE_D.Text = ((DateTime)report_h.FaxRcvDate).ToString("dd");

                REPORT_YY.Text = report_h.ReportYear;
                REPORT_MM.Text = report_h.ReportMonth;
                REPORT_DD.Text = report_h.ReportDay;

                UNION_CODE.Text = report_h.UnionCode;
                UNION_NAME.Text = union.OfficeName;

                if (report_h.BranchCode == 0)
                {
                    BRANCH_NAME.Text = "000 本店";
                }
                else
                {
                    BRANCH_NAME.Text = dao2.getDetail(report_h.UnionCode, report_h.BranchCode).officename;
                }

                TRN_JUCHU_TANTO_NAME.Text = union.TantoName;
                EST_COMP_CLSS1.Checked = true;

                TRADER_REG_NO1.Text = report_h.TraderRegNo1;
                TRADER_REG_NO2.Text = report_h.TraderRegNo2;

               // trader_label.Text = dao3.getDetail(union.TraderRegNo).TraderName;

                if (report_h.ReportClss.Equals("10"))
                {
                    RCV_P.Visible = false;
                }
                else
                {
                    RCV_P.Visible = true;
                }
                
            }

        }
        catch (Exception exx)
        {
            LogManager.error("seal_seal_report_represen_detail " + exx.Message + " " + exx.StackTrace);
        }
    }

    protected void login_confirm(object sender, EventArgs e)
    {
        try
        {
            Report rpt = new Report();
            LoginModel loginModel = (LoginModel)Session["loginModel"];

            TrnReporth model = (TrnReporth)Session["CHECKED_REPORT_H"];
            //報告見出セット(TRN_Report_H)
            //model.UnionCode = loginModel.Unioncode;
            //model.BranchCode = (short)loginModel.Branchcode;
            model.ReportNo = "";
            //受信日
            model.FaxRcvYear = SEAL_REPORT_REPRESEN_RCV_DATE_Y.Text;
            model.FaxRcvMonth = SEAL_REPORT_REPRESEN_RCV_DATE_M.Text;
            model.FaxRcvDay = SEAL_REPORT_REPRESEN_RCV_DATE_D.Text;
            if (StringUtil.isValidDate(SEAL_REPORT_REPRESEN_RCV_DATE_Y.Text, SEAL_REPORT_REPRESEN_RCV_DATE_M.Text, SEAL_REPORT_REPRESEN_RCV_DATE_D.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                model.FaxRcvDate = Convert.ToDateTime((int.Parse(model.FaxRcvYear) + 2000) + "/" + int.Parse(model.FaxRcvMonth) + "/" + int.Parse(model.FaxRcvDay), culture);

            }
            

            model.ReportTantoName = TRN_JUCHU_TANTO_NAME.Text;
            model.EstCompClss = EST_COMP_CLSS1.Checked ? "0" : EST_COMP_CLSS2.Checked ? "1" : null;
            model.ConsCompName = CONS_COMP_NAME.Text;
            model.ConsCompTelno = CONS_COMP_TELNO.Text;
            model.ConsCompTelno = CONS_COMP_TELNO.Text;
            model.HallName = HALL_NAME.Text;
            model.HallPrefCode = short.Parse(HALL_PREF_CODE.Text);
            model.HallCity = HALL_CITY.Text;
            model.HallAddress = HALL_ADDRESS.Text;
            model.HallTel = HALL_TEL.Text;
            model.MachineName = MACHINE_NAME.Text;
            model.MachineQty = MACHINE_QTY.Text.Trim().Length == 0 ? -1 : int.Parse(MACHINE_QTY.Text);
            model.EstYear = EST_YEAR.Text;
            model.EstMonth = EST_MONTH.Text;
            model.EstDay = EST_DAY.Text;
            if (StringUtil.isValidDate(EST_YEAR.Text, EST_MONTH.Text, EST_DAY.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                model.EstDate = Convert.ToDateTime((int.Parse(EST_YEAR.Text) + 2000) + "/" + int.Parse(EST_MONTH.Text) + "/" + int.Parse(EST_DAY.Text), culture);

            }
            model.TraderRegNo1 = TRADER_REG_NO1.Text;
            model.TraderRegNo2 = TRADER_REG_NO2.Text;
            model.TraderRegNo = TRADER_REG_NO1.Text + TRADER_REG_NO2.Text;
            model.ChiefNo1 = CHIEF_NO1.Text;
            model.ChiefNo2 = CHIEF_NO2.Text;
            model.ChiefNo = CHIEF_NO1.Text + CHIEF_NO2.Text;
            model.ChiefName = CHIEF_NAME.Text;
            model.Worker1Name = WORKER1_NAME.Text;
            model.Worker2Name = WORKER2_NAME.Text;
            model.Worker3Name = WORKER3_NAME.Text;
            model.Worker4Name = WORKER4_NAME.Text;
            model.Worker5Name = WORKER5_NAME.Text;
            model.Worker6Name = WORKER6_NAME.Text;
            model.ReportYear = REPORT_YY.Text;
            model.ReportMonth = REPORT_MM.Text;
            model.ReportDay = REPORT_DD.Text;
            if (StringUtil.isValidDate(REPORT_YY.Text, REPORT_MM.Text, REPORT_DD.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                model.ReportDate = Convert.ToDateTime((int.Parse(model.ReportYear) + 2000) + "/" + int.Parse(model.ReportMonth) + "/" + int.Parse(model.ReportDay), culture);

            }


            rpt.setReportH(model);

            TrnReportd[] models = new TrnReportd[5];
            TrnReportd tmpmodel = null;
            TrnReportdId id = null;

            tmpmodel = new TrnReportd();
            id = new TrnReportdId();
            id.ReportNo = "";
            id.ReportRecNo = 1;
            tmpmodel.Id = id;
            tmpmodel.SealStrNo = SEAL_STR_NO1.Text;
            tmpmodel.SealEndNo = SEAL_END_NO1.Text;
            tmpmodel.MachineClss = MACHINE_CLSS11.Checked ? "1" : MACHINE_CLSS12.Checked ? "2" : null;
            models[0] = tmpmodel;

            tmpmodel = new TrnReportd();
            id = new TrnReportdId();
            id.ReportNo = "";
            id.ReportRecNo = 2;
            tmpmodel.Id = id;
            tmpmodel.SealStrNo = SEAL_STR_NO2.Text;
            tmpmodel.SealEndNo = SEAL_END_NO2.Text;
            tmpmodel.MachineClss = MACHINE_CLSS21.Checked ? "1" : MACHINE_CLSS22.Checked ? "2" : null;
            models[1] = tmpmodel;

            tmpmodel = new TrnReportd();
            id = new TrnReportdId();
            id.ReportNo = "";
            id.ReportRecNo = 3;
            tmpmodel.Id = id;
            tmpmodel.SealStrNo = SEAL_STR_NO3.Text;
            tmpmodel.SealEndNo = SEAL_END_NO3.Text;
            tmpmodel.MachineClss = MACHINE_CLSS31.Checked ? "1" : MACHINE_CLSS32.Checked ? "2" : null;
            models[2] = tmpmodel;

            tmpmodel = new TrnReportd();
            id = new TrnReportdId();
            id.ReportNo = "";
            id.ReportRecNo = 4;
            tmpmodel.Id = id;
            tmpmodel.SealStrNo = SEAL_STR_NO4.Text;
            tmpmodel.SealEndNo = SEAL_END_NO4.Text;
            tmpmodel.MachineClss = MACHINE_CLSS41.Checked ? "1" : MACHINE_CLSS42.Checked ? "2" : null;
            models[3] = tmpmodel;

            tmpmodel = new TrnReportd();
            id = new TrnReportdId();
            id.ReportNo = "";
            id.ReportRecNo = 5;
            tmpmodel.Id = id;
            tmpmodel.SealStrNo = SEAL_STR_NO5.Text;
            tmpmodel.SealEndNo = SEAL_END_NO5.Text;
            tmpmodel.MachineClss = MACHINE_CLSS51.Checked ? "1" : MACHINE_CLSS52.Checked ? "2" : null;
            models[4] = tmpmodel;

            rpt.setReportD(models);

            TRN_ReportManager validation = new TRN_ReportManager();
            List<string> errorCodeList = validation.checkInsertReportRepresen2(rpt, Resources.Resource.ResourceManager, loginModel);
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count == 0)
            {
                Session["report"] = rpt;
                Response.Redirect("seal_report_represen_conf.aspx");
            }

        }
        catch (Exception e1)
        {
            LogManager.error("seal_report checkInsertReportRepresen2 error" + e1.Message + e1.StackTrace);
        }


    }

    protected void goBack(object sender, EventArgs e)
    {
        Response.Redirect("seal_report_represen.aspx");
    }
}
